#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

struct Student
{
    char num[ 7 ], name[ 9 ];
    int score;
};

int n, c;

bool operator < ( Student a, Student b )
{
    if ( c == 1 )
        return strcmp( a.num, b.num ) < 0;
    if ( c == 2 )
    {
        if ( strcmp( a.name, b.name ) == 0 )
            return strcmp( a.num, b.num ) < 0;
        return strcmp( a.name, b.name ) < 0;
    }
    if ( c == 3 )
    {
        if ( a.score == b.score )
            return strcmp( a.num, b.num ) < 0;
        return a.score < b.score;
    }
}

Student stu[ 100000 + 10 ];

void init( )
{
    int i;
    for ( i = 0; i < n; i++ )
        scanf("%s%s%d", stu[ i ].num, stu[ i ].name, &stu[ i ].score);
}

void print( )
{
    int i;
    for ( i = 0; i < n; i++ )
        printf("%s %s %d\n", stu[ i ].num, stu[ i ].name, stu[ i ].score);
}

int main( )
{
    int i = 1;
    while ( scanf("%d%d", &n, &c) != EOF && n )
    {
        printf("Case %d:\n", i++);
        init( );
        sort( stu, stu + n );
        print( );
    }
    return 0;
}
